home *** CD-ROM | disk | FTP | other *** search
- /*
- * mipsABI porting examples
- *
- * ptysetup - demonstrate the method for setting up pseduo-ttys
- *
- * open_master(): open the master side of pty
- * open_slave(): open the slave side of the pty
- *
- */
- #define SGI4_PTY
-
- #include <sys/types.h>
-
- #ifdef HAVE_TERMIO
- # include <termio.h>
- #else
- # include <sgtty.h>
- #endif
-
- #if defined(hpux) || (defined(sun) && defined(SVR4))
- # include <sys/file.h>
- #endif
-
- #ifdef SCO
- # include <sys/fcntl.h>
- #endif
-
- #ifdef STR_PTY
- # include <sys/stropts.h>
- # include <signal.h>
- #endif
-
- #if defined(PTC_PTY)
- # include <sys/stropts.h>
- # include <sys/stat.h>
- # include <sys/sysmacros.h>
- #elif defined(sgi)
- # include <fcntl.h>
- # include <signal.h>
- # include <sys/stat.h>
- #endif
-
- #include <stdio.h>
-
- /* prototypes */
- static int open_master (void);
- static int open_slave (int);
-
- #ifdef STR_PTY
- /* STREAMS ptys need separate read and write fd's */
- FILE *ex_fpin = NULL;
- FILE *ex_fpout = NULL;
- #else
- FILE *ex_fp = NULL;
- #endif
-
- int ex_pid = 0; /* example process id */
- static int ex_InputId; /* example input id */
-
- #if defined(PTC_PTY)
- static char pty[] = "/dev/ptc"; /* clone dev of pseudo-terminal */
- static char tty[15]; /* slave side of pseudo-terminal */
- int pts_master; /* master filedes */
- char ex_tty[15];
- #endif
-
- #if defined(IRIX4_PTY)
- char *_getpty(int*, int, mode_t, int);
- static char tty[15];
- static char pty[15];
- char ex_tty[15];
- #endif
-
- #ifdef STR_PTY
- static char pty[] = "/dev/ptmx";
- static char tty[15]; /* slave side of pseudo-terminal */
- int pts_master; /* master filedes */
- char ex_tty[15];
- #endif
-
- #if !defined(STR_PTY) && !defined(PTC_PTY) && !defined(IRIX4_PTY)
- static char pty[11] = "/dev/pty??"; /* master side of pseudo-terminal */
- static char tty[11] = "/dev/tty??"; /* slave side of pseudo-terminal */
- char ex_tty[11] = "/dev/tty??";
- #endif
-
- static int hold_slave = -1;
- extern char *ex_prompt;
-
- /*
- * dummy main for compiling
- */
- main ()
- {
- open_master ();
- }
-
- /*
- * This program talks to a child process through a pseudo terminal which
- * is a pair of master and slave devices: /dev/pty?? and /dev/tty??, where
- * ?? goes from p0 to sf (system dependent). The pty is opened for both
- * read and write. Some systems use SYSV STREAMS based pty's. For these
- * define STR_PTY. Some use /dev/ptc based pty's, for those use PTC_PTY.
- */
- static int open_master()
- {
- int i, master, fd;
- char c;
-
- #ifdef IRIX4_PTY
- char *line;
- SIG_PF oldintr;
- #endif
-
- #if defined(PTC_PTY)
- if ((pts_master = open(pty, O_RDWR)) >= 0)
- {
- struct stat sb;
-
- if (fstat(pts_master, &sb) < 0)
- close(pts_master);
- else
- {
- hold_slave = minor(sb.st_rdev);
- return(pts_master);
- }
- }
- #endif
-
- #ifdef IRIX4_PTY
- if ((oldintr = signal(SIGCHLD, SIG_DFL)) == SIG_ERR)
- {
- perror("bad return from signal");
- exit(1);
- }
- line = _getpty(&master, O_RDWR | O_NDELAY, 0600, 0);
- if (signal(SIGCHLD, oldintr) == SIG_ERR)
- {
- perror("bad return from signal");
- exit(1);
- }
- if(line)
- {
- strcpy(tty, line);
- strcpy(ex_tty, line);
- return(master);
- }
- #endif
-
- #ifdef STR_PTY
- if ((pts_master = open(pty, O_RDWR)) >= 0)
- {
- grantpt(pts_master);
- unlockpt(pts_master);
- return(pts_master);
- }
- #endif
-
- #if !defined(STR_PTY) && !defined(PTC_PTY) && !defined(IRIX4_PTY)
- for (c='p'; c<'z'; c++)
- {
- for (i=0; i<16; i++)
- {
- pty[8] = c;
- pty[9] = "0123456789abcdef"[i];
- tty[8] = c;
- tty[9] = pty[9];
- /*
- * I need to check that tty is not the same device we are using
- * for child's pseudo-terminal. Xtty has to find its own. If this
- * it the first pseudo-tty then ex_tty[0] is "??" otherwise it
- * keeps the last letters of child's tty.
- */
- if (strcmp(&ex_tty[8], &tty[8]) &&
- (master = open(pty, O_RDWR)) >= 0)
- {
- if ((hold_slave = open(tty, O_RDWR)) >= 0)
- return (master);
- else close(master);
- }
- }
- }
- #endif
-
- fprintf(stderr, "ptysetup: all ptys in use\n");
- return(-1);
- }
-
- static int open_slave(int pts_slave)
- {
-
- #ifdef STR_PTY
- char *ptsname();
- char *slavename;
-
- slavename = ptsname (pts_master); /* get name of slave */
- strcpy (tty, slavename); /* copy over to save area */
- strcpy (ex_tty, slavename);
- if ((pts_slave = open(tty, O_RDWR)) >= 0)
- {
- /*XXX todo: need to make sure these succeed */
- ioctl (pts_slave, I_PUSH, "ptem");
- ioctl (pts_slave, I_PUSH, "ldterm");
- ioctl (pts_slave, I_PUSH, "ttcompat");
- return(pts_slave);
- }
- #endif
-
- #if defined(PTC_PTY)
- sprintf(tty, "/dev/ttyq%d", hold_slave);
- if ((pts_slave = open(tty, O_RDWR)) >= 0) {
- strcpy(ex_tty, tty);
- return(pts_slave);
- }
- #endif
-
- #ifdef IRIX4_PTY
- if ((pts_slave = open(tty, O_RDWR)) >= 0) {
- strcpy(ex_tty, tty);
- return(pts_slave);
- }
- #endif
-
- #if !defined(STR_PTY) && !defined(PTC_PTY) && !defined(IRIX4_PTY)
- return(hold_slave);
- #else
- fprintf(stderr, "ptysetup: failed to open slave pty\n");
- return(-1);
- #endif
-
- }
-